package xyj.sparrow.client.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyj.sparrow.client.NettyClient;
import xyj.sparrow.client.proxy.HandlerBeanDefinitionRegistry;
import xyj.sparrow.client.proxy.RpcProxy;
import xyj.sparrow.client.util.SpringContextUtil;

@Configuration
@EnableConfigurationProperties(AppConf.class)
public class AutoConfiguration implements ApplicationContextAware {
    @Bean
    public SpringContextUtil springContextUtil() {
        return new SpringContextUtil();
    }

    @Bean
    public NettyClient nettyClient() {
        return new NettyClient();
    }

    @Bean
    public HandlerBeanDefinitionRegistry handlerBeanDefinitionRegistry() {
        return new HandlerBeanDefinitionRegistry();
    }

    @Bean
    public RpcProxy rpcProxy(NettyClient nettyClient) {
        return new RpcProxy(nettyClient);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil springContextUtil = new SpringContextUtil();
        springContextUtil.setApplicationContext(applicationContext);
    }

}
